package com.sunlg.smarthome.base;

import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import butterknife.ButterKnife;

/**
 * Created by sunlg on 2017/2/22.
 */

public abstract class BaseFragment extends Fragment{
    private ActivityManager activityManager;
    private Context mContext;
    private ProgressDialog progressDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResourceId(), container, false);

        mContext = getContext();
        activityManager = ActivityManager.getInstance();

        ButterKnife.bind(view);
        init();
        initView(view);
        return view;
    }

    /**
     * 获取布局文件
     * @return
     */
    protected abstract int getLayoutResourceId();

    /**
     * 数据初始化
     */
    protected void init(){}

    /**
     * 界面初始化
     */
    protected void initView(View view){}

    public void toast(String msg){
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    public void showLoadBar(String message){
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage(message);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }
    public void hideLoadBar(){
        progressDialog.cancel();
    }

    /**
     * 页面转跳函数，不结束前一页面
     * @param classObj
     */
    public void overlay (Class<?> classObj) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(mContext, classObj);
        startActivity(intent);
    }

    /**
     * 页面转跳函数，不结束前一页面
     * @param classObj
     * @param params  传递数据
     */
    public void overlay (Class<?> classObj, Bundle params) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(mContext, classObj);
        intent.putExtras(params);
        startActivity(intent);
    }

    /**
     * 页面转跳函数，结束前一页面
     * @param classObj
     */
    public void forward (Class<?> classObj) {
        Intent intent = new Intent();
        intent.setClass(mContext, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        activityManager.finishActivity();
    }

    /**
     * 页面转跳函数，结束前一页面
     * @param classObj
     * @param params  传递的数据
     */
    public void forward (Class<?> classObj, Bundle params) {
        Intent intent = new Intent();
        intent.setClass(mContext, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(params);
        startActivity(intent);
        activityManager.finishActivity();
    }
}
